<template>
  <div class="loufengAreaManagement">
    <h3>楼凤地区管理</h3>
    <!-- 查询条件 -->
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入省份" v-model="search1">
          <template slot="prepend">省份</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>状态</label>
        <el-select v-model="search2" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <!-- 列表 -->
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
       <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="province"
          label="省份"
        ></el-table-column>
        <el-table-column header-align="center" align="center" label="城市">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.son_city.length == 1"
              type="primary"
              size="mini"
            >{{ scope.row.son_city[0].city}} {{scope.row.son_city[0].online_all}}</el-button>
            <el-tooltip placement="right" v-if="scope.row.son_city.length>1">
              <el-button type="primary" size="mini">
                {{scope.row.son_city[0].city}} {{scope.row.son_city[0].online_all}}
                <span
                  v-if="scope.row.son_city.length > 1"
                >...</span>
              </el-button>
              <div slot="content" class="tooltipList">
                <el-button
                  v-for="(item, index) in scope.row.son_city"
                  :key="index"
                  type="primary"
                  size="mini"
                >{{ item.city }} {{item.online_all}}</el-button>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="floor_all"
          label="楼凤数量"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="online_all"
          label="上架楼凤数量"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="sort" label="排序"></el-table-column>
        <el-table-column header-align="center" align="center" label="操作"  fixed="right">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="mini"
              @click="handleOpen(scope.$index, scope.row)"
              v-show="scope.row.is_display==0"
              v-if="permissions_obj.id_193||permissions_obj.id_all"
            >打开</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="handleClose(scope.$index, scope.row)"
              v-show="scope.row.is_display==1"
              v-if="permissions_obj.id_193||permissions_obj.id_all"
            >关闭</el-button>
            <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)" v-if="permissions_obj.id_194||permissions_obj.id_all">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- 编辑弹窗 -->
    <el-dialog title="修改排序" :visible.sync="dialogVisible" width="40%" center class="dialog">
      <el-form ref="dialogForm" :model="dialogForm" :rules="dialogRule" label-width="100px">
        <el-form-item label="标签排序：" prop="editSort">
          <el-input placeholder="请输入排序值" v-model="dialogForm.editSort" clearable></el-input>
        </el-form-item>
        <div class="dialogBtn">
          <el-button type="primary" size="small" @click="submitForm('dialogForm')">提交</el-button>
          <el-button type="info" size="small" @click="cancelForm">取消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "loufengAreaManagement",
  data() {
    return {
      options: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 0,
          label: "关闭"
        },
        {
          value: 1,
          label: "打开"
        }
      ],
      search1: "",
      search2: "",
      tableData: [],
      /**
       * 分页
       * @param {number} [currentPage] - 当前选中的页码【当前页码】
       */
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      /**
       * 编辑弹出框
       * @param {boolean} [dialogVisible]       - 是否显示编辑弹出框
       * @param {boolean} [dialogForm]          - 编辑弹出框表单
       * @param {number}  [dialogForm.editSort] - 编辑弹出框表单-标签排序
       * @param {object}  [dialogRule]          - 编辑弹出框表单验证规则
       */
      dialogVisible: false,
      dialogForm: {
        editSort: undefined
      },
      dialogRule: {
        editSort: [
          { required: true, message: "请输入标签排序", trigger: "blur" },
          { type: "number", message: "标签排序必须为数字" }
        ]
      },
      initData: {},
       // 权限管理
      permissions: [],
      permissions_obj: {}
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.initDataList();
  },
  methods: {
    searchMethod(){
      this.currentPage=1;
      this.initDataList();
    },
    /**
     * 初始化列表
     */
    async initDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage,
          page_size: this.pageSize,
          search: {
            province: this.search1,
            is_display: this.search2
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.floorTypeLists,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    /**
     * 【按钮】打开按钮点击事件
     * @param {object} [row] - 当前行信息
     * @function [openOrCloseFn] - 关闭打开接口方法调用
     */
    handleOpen(idnex,row) {
      this.dialogForm.id=row.id;
      this.$confirm("是否继续该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.openOrCloseFn(row.is_display==0?1:0);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消该操作"
          });
        });
    },
    /**
     * 【按钮】关闭按钮点击事件
     * @param {object} [row] - 当前行信息
     * @function [openOrCloseFn] - 关闭打开接口方法调用
     */
    handleClose(idnex,row) {
      this.dialogForm.id=row.id;
      this.$confirm("是否继续该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.openOrCloseFn(row.is_display==0?1:0);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消该操作"
          });
        });
    },
    //编辑
    handleEdit(index, row) {
      this.dialogVisible = true;
      this.dialogForm.id = row.id;
      this.dialogForm.editSort = row.sort;
    },
    /**
     * 【按钮】打开/关闭接口方法
     */
    async openOrCloseFn(state) {
      let parames = {
        handshake: "",
        data: {
          id: this.dialogForm.id,
          is_display:state,
        }
      };
      let res = await this.$axios.post(
        this.$api.floorTypeSet_display,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.initDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    /**
     * 【分页】切换每页显示的条数
     * @param {number} [val]         - 当前选中的数值
     * @param {number} [currentPage] - 当前选中的页码
     * @param {number} [pageSize]    - 每页显示的条数
     */
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.initDataList();
    },
    /**
     * 【分页】切换页码
     * @param {number} [val]         - 当前选中的数值
     * @param {number} [currentPage] - 当前选中的页码
     */
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initDataList();
    },
    /**
     * 【编辑弹出框】初始化表单验证+初始化表单数据
     */
    initDialog() {
      this.$refs.dialogForm.clearValidate();
    },
    /**
     * 【编辑弹出框】提交
     * @param {string} [formName] - 弹出框表单名称
     * @function [initDataList] - 初始化列表
     */
    async submitForm() {
      let parames = {
        handshake: "",
        data: {
          id: this.dialogForm.id,
          sort: this.dialogForm.editSort
        }
      };
      let res = await this.$axios.post(
        this.$api.floorTypeUpdate,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.initDataList();
        this.dialogVisible = false;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    /**
     * 【编辑弹出框】取消提交
     * @function [initDialog] - 初始化弹出框表单方法
     * @param {boolean} [dialogVisible] - 是否显示弹出框
     */
    cancelForm() {
      this.initDialog();
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
/* 更多城市列表 */
.tooltipList {
  width: 300px;
}
.tooltipList .el-button {
  margin: 2.5px 5px 2.5px 0;
}
</style>